package com.adv.ems.model.vo;

import cn.hutool.core.date.DateUtil;
import com.adv.ems.db.entity.CollectPointDO;
import com.adv.ems.db.entity.EnergyInfoDO;
import com.adv.ems.db.entity.MeterDO;
import com.adv.ems.utils.EmsUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
public class MeterHealthVO {
    private List<Info> list;

    public MeterHealthVO() {
        this.list = new ArrayList<Info>();
    }

    @Data
    public static class Info {
        private Long meterId;
        private String meterCode;
        private String meterName;
        private String energyName;
        private Integer pointNum;
        private String lastUploadDate;
        private Boolean health;
        private String stateDesc;

        public Info(MeterDO meter, EnergyInfoDO energy, List<CollectPointDO> points, String acceptLanguage) {
            this.meterId = meter.getId();
            this.meterCode = meter.getCode();
            this.meterName = meter.getName();
            if (energy != null) {
                this.energyName = EmsUtil.multilingual(energy.getNameInfo(), acceptLanguage);
            }
            this.pointNum = points.size();
            Date last = null;
            for (CollectPointDO point : points) {
                if (last == null) {
                    last = point.getLastUploadTime();
                }
                if (last != null && point.getLastUploadTime() != null && point.getLastUploadTime().after(last)) {
                    last = point.getLastUploadTime();
                }
            }
            Map.Entry<Boolean, String> deviceState = EmsUtil.getDeviceState(last);
            this.health = deviceState.getKey();
            this.stateDesc = deviceState.getValue();
            this.lastUploadDate = last == null ? null : DateUtil.formatDate(last);
        }
    }
}
